package cn.bincker.mybatis.encrypt.converter.impl;

import cn.bincker.mybatis.encrypt.exception.InvalidDataException;

import java.nio.ByteBuffer;

public class FloatEncryptConverter extends BaseEncryptConverter<Float> {
    @Override
    public Float convertNonNull(byte[] data) {
        if (data.length >= Double.BYTES){
            return (float) ByteBuffer.wrap(data).getDouble();
        }
        else if (data.length >= Float.BYTES){
            return ByteBuffer.wrap(data).getFloat();
        } else throw new InvalidDataException("long need " + Float.BYTES + " bytes but got " + data.length);
    }

    @Override
    public byte[] convertNonNull(Float object) {
        byte[] result = new byte[Float.BYTES];
        ByteBuffer.wrap(result).putFloat(object);
        return result;
    }
}
